package com.mbatis.leeCode;

import org.junit.Test;

/**
 * @Classname addOne
 * @Date 2021/6/10 18:19  66 加一
 * @Created by zhang.yuming
 */
public class addOne {

    public static void main(String[] args) {
        int[] nums = {1,3,5,6,8,9};
//        int[] array = add(nums);
//        System.out.println("array="+array);
        int[] array2 = plusOne(nums);
        System.out.println("array2="+array2);
    }

    private static int[] plusOne(int[] nums) {
        int len = nums.length;
        for (int i = len -1; i >= 0; i--) {
            nums[i]++;
            nums[i] %= 10;
            if (nums[i] != 0){
                return nums;
            }
        }
        nums = new int[len + 1];
        nums[0] = 1;
        return nums;
    }

    private static int[] add(int[] nums) {
        int length = nums.length-1;
        for (int i = 0; i < nums.length-1; i++) {
            if (nums[length]<9){
                int i1 = nums[length] + 1;
                nums[length] = i1;
            }else {
                nums[length] = 1;
                nums[nums.length] = 0;
            }
        }
        return nums;
    }

    @Test
    public void plusOne2() {
        int[] digits = {1,3,5,6,8,9};
        int len = digits.length;
        for(int i = len - 1; i >= 0; i--) {
            digits[i]++;
            digits[i] %= 10;
            if(digits[i]!=0)
                System.out.println("di="+digits);
        }
        digits = new int[len + 1];
        digits[0] = 1;
        System.out.println("di="+digits);
    }

}
